<?php

namespace app\db\controller;

use app\db\model\DbUsercareAuth;
use fast\Tree;
use think\Db;

class DbTeamDoctor extends Common
{
    protected $noNeedLogin = [];
    protected $noNeedRight = [];
    protected $model = null;
    protected $rulelist = [];

    public function _initialize()
    {
        parent::_initialize();
        //移除HTML标签
        $this->request->filter('trim,strip_tags,htmlspecialchars');
        $this->model = model('DbUsercareRelation');
        if (!$this->request->isPost()) {
            $this->error(__('No Post request'));
        }
    }

    /**
     * @Notes:getTeamList   获取当前库医生成员列表
     * @author:WuXiaoMing
     * Date:2021/8/21
     * Time:10:08 上午
     */
    public function getTeamList()
    {
        $database_id = input('post.database_id');
        if (!$database_id) $this->error(__('Invalid parameters'));
        $search = input('post.search') ? input('post.search') : '';
        # 筛选条件不能包含英文单引号
        if (strstr($search, "'")) $this->error('筛选条件错误');
        $page  = input('post.page') ? input('post.page') : 1;
        $limit = input('post.limit') ? input('post.limit') : 10;
        //查询所有医务人员
        $where = [
            'database_id'     => $database_id,
            'relation_status' => 1,
        ];
        if (!empty($search)) {
            $where['name'] = ['like', '%' . $search . '%'];
        }
        $creator_id = model('DbDatabase')->getDetails($database_id,'usercare_id'); //创建数据库的医务ID
        //查询所有医务人员
        $ret_info['list']  = db('db_usercare_relation')->alias('r')
            ->field('r.id as relation_id,r.usercare_id,u.name,u.account,r.relation_auth,r.relation_status,r.join_time')
            ->join('db_usercare u', 'r.usercare_id=u.id')
            ->where($where)
            ->order('join_time Desc')
            ->page($page, $limit)
            ->select();
        foreach ($ret_info['list'] as $key=>$value){
            if ($value['usercare_id']==$creator_id){
                $ret_info['list'][$key]['creator'] = true;
            }else {
                $ret_info['list'][$key]['creator'] = false;
            }
        }
        $ret_info['total'] = db("db_usercare_relation")->where($where)->count();
        $this->success(__('To be successful'), $ret_info);
    }


    /**
     * @Notes:inviteDoctor  获取邀请医生列表
     * @author:WuXiaoMing
     * Date:2021/8/20
     * Time:9:54 上午
     */
    public function inviteDoctor()
    {
        $database_id = input('post.database_id');
        if (!$database_id) $this->error(__('Invalid parameters'));
        $search = input('post.search') ? input('post.search') : '';
        # 筛选条件不能包含英文单引号
        if (strstr($search, "'")) $this->error('筛选条件错误');
        $page['page']  = input('post.page') ? input('post.page') : 1;
        $page['limit'] = input('post.limit') ? input('post.limit') : 10;
        //查询所有医务人员
        $where = [
            'state'  => 1,
            'status' => 1,
        ];
        if (!empty($search)) {
            $where['name'] = ['like', '%' . $search . '%'];
        }
        //查询所有医务人员
        $ret_info = model('DbUsercare')->get_lists('id,name,sex,hospital,department,titles', $where, 'create_time desc', $page);
        //查询团队已关联的医生id
        $relation_info = model('DbUsercareRelation')->where(['database_id' => $database_id, 'relation_status' => 1])->column('usercare_id');
        foreach ($ret_info['lists'] as $k => $v) {
            if (in_array($v['id'], $relation_info)) {
                $ret_info['lists'][$k]['relation_status'] = 1;
            } else {
                $ret_info['lists'][$k]['relation_status'] = 0;
            }
        }
        $this->success('To be successful', $ret_info);
    }

    /**
     * @Notes:add_relation_doctor   邀请医务接口
     * @author:WuXiaoMing
     * Date:2021/8/20
     * Time:5:09 下午
     */
    public function addRelationDoctor()
    {
        $database_id        = input('post.database_id');
        $usercare_id        = input('post.usercare_id');
        $invite_usercare_id = input('post.invite_usercare_id');
        $auth               = input('post.auth') ? input('post.auth') : '3';
        if (!$database_id || !$usercare_id || !$invite_usercare_id) $this->error(__('Invalid parameters'));
        $data = [
            'database_id'        => $database_id,
            'usercare_id'        => $usercare_id,
            'relation_auth'      => $auth,
            'invite_usercare_id' => $invite_usercare_id,
            'join_time'          => time(),
        ];
        $res  = model('DbUsercareRelation')->addData($data);
        if ($res) {
            $this->success(__('Operation completed'));
        } else {
            $this->error(__('Operation failed'));
        }
    }

    /**
     * @Notes:delRelationDoctor   移除医务接口
     * @author:WuXiaoMing
     * Date:2021/8/20
     * Time:5:20 下午
     */
    public function delRelationDoctor()
    {
        $relation_id = input('post.relation_id');
        if (!$relation_id) $this->error(__('Invalid parameters'));
        $data  = [
            'leave_time'      => time(),
            'relation_status' => 0,
        ];
        $where = ['id' => $relation_id];
        $res   = model('DbUsercareRelation')->saveData($where, $data);
        if ($res) {
            $this->success(__('Operation completed'));
        } else {
            $this->error(__('Operation failed'));
        }
    }

    /**
     * @Notes:editDcotorAuth    修改医生权限
     * @author:WuXiaoMing
     * Date:2021/8/21
     * Time:3:53 下午
     */
    public function editDcotorAuth()
    {
        $relation_id   = input('post.relation_id');
        $relation_auth = input('post.relation_auth/a');
        if (!$relation_id || !$relation_auth) $this->error(__('Invalid parameters'));
        $data  = [
            'relation_auth' => implode(',', $relation_auth),
        ];
        $where = ['id' => $relation_id];
        $res   = model('DbUsercareRelation')->saveData($where, $data);
        if ($res) {
            $this->success(__('Operation completed'));
        } else {
            $this->error(__('Operation failed'));
        }
    }


}
